<?php

namespace App\Http\Controllers\web;

use App\Helpers\Image;
use App\Http\Controllers\Controller;
use App\Repository\RepositoryInterface\BargainInterface;
use Illuminate\Http\Request;

//砍价类
class BargainController extends Controller
{

    protected $bargain;

    public function __construct(BargainInterface $bargain){
        $this->bargain = $bargain;
    }

    /**
     * 砍价商品
     * @param Request $request  获取参数
     * @return array
     */
    public function index(Request $request)
    {
        try {
            $data = $this->bargain->selAll();
            return ['code'=>20000,'msg'=>'success','data'=>$data];
        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }
    }

    /**
     * 砍价商品列表
     * @return mixed
     */
    public function bargainList(Request $request)
    {
        try {
            $page = $request->get('page');
            $data = $this->bargain->selList($page);
            return ['code'=>20000,'msg'=>'success','data'=>$data];
        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }
    }

    /**
     * 修改状态
     * @param Request $request
     */
    public function bargainStatus(Request $request)
    {
        try {
            $id = (int)$request->get('id');
            $status = (int)$request->get('status');
            if(empty($id))
            {
                return ['code'=>5000,'msg'=>'参数错误','data'=>[]];
            }
            $res = $this->bargain->upSatatu($id,$status);
            return ['code'=>20000,'msg'=>'success','data'=>[]];
        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }

    }

    /**
     * @param Request $request
     * @return array  砍价删除
     */
    public function bargainDel(Request $request)
    {

        try {
            $id = (int)$request->get('id');
            if(empty($id))
            {
                return ['code'=>5000,'msg'=>'参数错误','data'=>[]];
            }
            $res = $this->bargain->bargainDel($id);
            return ['code'=>20000,'msg'=>'success','data'=>[]];
        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }
    }

    /**\
     * @param Request $request  接收数据
     * @return array  砍价商品添加
     */
    public function bargainCreate(Request $request)
    {
        try {
            $data = $request->all();
            $data = [
                'title'=>$data['title'],
                "product_id" => $data['bargain_num'],
                "info" => $data['info'],
                "price" => $data['price'],
                "unit_name" => $data['unit_name'],
                "bargain_num" => $data['bargain_num'],
                "num" => $data['num'],
                "start_time" => date('ymd'),
                "stop_time" => date('ymd'),
                "temp_id" => $data['temp_id'],
                "status" => $data['status'],
                "image" => $data['cover'],
                "images" => $data['cover'],
            ];
            if(empty($data))
            {
                return ['code'=>5000,'msg'=>'参数错误','data'=>[]];
            }
            $res = $this->bargain->bargainCreate($data);
            return ['code'=>20000,'msg'=>'success','data'=>[]];
        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }

    }

    /**
     * @param Request $request
     * @return array  数据获取修改数据
     */
    public function bargainRevamp(Request $request)
    {

        try {
            $id = (int)$request->get('id');
            if(empty($id))
            {
                return ['code'=>5000,'msg'=>'参数错误','data'=>[]];
            }
            $data = $this->bargain->bargainDetail($id);
            return ['code'=>20000,'msg'=>'success','data'=>$data];

        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }
    }

    /**
     * @param Request $request
     * @return array  砍价商品修改
     */
    public function bargainUpdate(Request $request)
    {
        try {
            $data = $request->post();
            $id = (int)$request->get('id');
            if(empty($id) || empty($data))
            {
                return ['code'=>5000,'msg'=>'参数错误','data'=>[]];
            }
            $res = $this->bargain->bargainUpdate($id,$data);
            return ['code'=>20000,'msg'=>'success','data'=>[]];
        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }

    }

    /**、
     * @param Request $request
     * @return array  搜索
     * @return array  砍价商品修改
     */
    public function getOrderAdd(Request $request)
    {
        try {
            $data = $request->post();
            if(empty($data))
            {
                return ['code'=>5000,'msg'=>'参数错误','data'=>[]];
            }
            $res = $this->bargain->getOrderAdd($data);
            return ['code'=>20000,'msg'=>'success','data'=>[]];
        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }

    }


    /**
     * @param $data  数据
     * @return mixed 搜索状态

     */
    public function searchData(Request $request)
    {
        try {
            $bargainStatus = (int)$request->get('bargainStatus');
            $id = (int)$request->get('id');
            $data = $this->bargain->searchData($bargainStatus,$id);
            return ['code'=>20000,'msg'=>'success','data'=>$data];
        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }

    }
    /**
     * @param $data  数据
     * @return mixed 搜索时间
     */
    public function searchListData(Request $request)
    {
        try {
            $status = $request->get('status');
            $date = $request->get('date');
            $data = $this->bargain->searchListData($status,$date);
            return ['code'=>20000,'msg'=>'success','data'=>$data];
        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }

    }

    /**
     * @return array 商品列表
     */
    public function shopList(Request $request)
    {
        try {
            $page = $request->get('page');
            $data = $this->bargain->shopList($page);
            return ['code'=>20000,'msg'=>'success','data'=>$data];
        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }
    }

    /**
     * @param Request $request
     * @return array 图片上传
     */
    public function uploadPictures(Request $request)
    {
        try {
            $cover = $request->file('cover');
            $image = new Image();
            $cover = $image->myUpfile($cover,'C38IfsOyr3No0evJ-ETfbfEsa-UIU3qk8SBS6r9v','Z9Fi2jgdMA2thzm3ATpwecqbUjVBy7emfA1GLXXx','lurongqiu','http://image.lurongqiu.cn/');
            return ['code'=>20000,'msg'=>'success','data'=>$cover];
        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }
    }
    /**
     * @param Request $request
     * @return array 图片上传
     */
    public function getSku(Request $request)
    {
        try {
            $id = $request->get('id',2);
            if($id == '')
            {
                return ['code'=>20000,'msg'=>'success','data'=>[]];
            }
            $data = $this->bargain->getSku($id);
            return ['code'=>20000,'msg'=>'success','data'=>$data];
        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }
    }
    /**
     * @param Request $request
     * @return array  订单列表
     */
    public function getOrder(Request $request)
    {
        try {
            $page = $request->get('page');
            $data = $this->bargain->getOrder($page);
            return ['code'=>20000,'msg'=>'success','data'=>$data];
        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }
    }

    /**
     * @param Request $request
     * @return array  砍价删除
     */
    public function orderDel(Request $request)
    {

        try {
            $id = (int)$request->get('id');
            if(empty($id))
            {
                return ['code'=>5000,'msg'=>'参数错误','data'=>[]];
            }
            $res = $this->bargain->orderDel($id);
            return ['code'=>20000,'msg'=>'success','data'=>[]];
        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }
    }
    /**
     * @param $data  数据
     * @return mixed 搜索订单
     */
    public function searchOrder(Request $request)
    {
        try {
            $name = $request->get('name');
            $data = $this->bargain->searchOrder($name);
            return ['code'=>20000,'msg'=>'success','data'=>$data];
        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }

    }
    /**
     * @param $data  数据
     * @return mixed 获取一条数据

     */
    public function getOrderOne(Request $request)
    {
        try {
            $id = $request->get('id');
            $data = $this->bargain->getOrderOne($id);
            return ['code'=>20000,'msg'=>'success','data'=>$data];
        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }

    }
    /**
     * @param $data  数据
     * @return mixed 设置

     */
    public function goodsSet(Request $request)
    {
        try {
            $data = $request->all();
            $data = $this->bargain->goodsSet($data);
            return ['code'=>20000,'msg'=>'success','data'=>[]];
        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }
    }

    /**
     * @return array 分销层级渲染
     */
    public function getInfinite()
    {
        try {
            $data = $this->bargain->getInfinite();
            $data =  $this->subTree($data);
            return ['code'=>20000,'msg'=>'success','data'=>$data];
        }catch (\Exception $exception){
            return ['code'=>5000,'msg'=>$exception,'data'=>[]];
        }
    }

    /**
     * @param $data
     * @param int $pid
     * @return array 层级处理
     */
    public function subTree($data,  $pid = 0) {
        // 返回的结果
        $arr = [];
        foreach ($data as $val) {
            // 给定的PID是当前记录的上级ID
            if ($pid == $val['pid']) {
                // 递归
                $val['sub'] = $this->subTree($data,$val['id']);
                $arr[] = $val;
            }
        }
        return $arr;
    }
}
